﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eis51.CampanyCMS.Model.DownLoadMgnt;
using Eis51.CampanyCMS.Business.DownLoadMgnt;
using Eis51.CampanyCMS.Model.DownLoadMgnt.QueryO;

namespace Eis51.Web.Admin.DownLoadFileMgnt
{
    public partial class list_download : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlType.DataSource = DownloadFileTypeBLL.GetInstance().Select();
                ddlType.DataTextField = "TypeName";
                ddlType.DataValueField = "DownLoadFileTypeID";
                ddlType.DataBind();
                btnSearch_Click(null, null);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DownloadFileQuery query = new DownloadFileQuery();
            query.Title = txtTitle.Text;
            query.BeginDate = txtBeginDate.Text;
            query.EndDate = txtEndDate.Text;
            query.TypeId = ddlType.SelectedValue;
            this.dgNews.Data = DownloadFileBLL.GetInstance().GetByCondition(query);
            this.dgNews.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {            
            Response.Redirect("edit_download.aspx?r=" + Server.UrlDecode(Request.RawUrl));
        }

        protected void dgNews_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                    DownloadFile obj = DownloadFileBLL.GetInstance().Get(new DownloadFile.Key(int.Parse(e.CommandArgument.ToString())));
                    DownloadFileBLL.GetInstance().DeleteObj(obj);
                    btnSearch_Click(null, null);
                    break;
                case "Modify":
                    string url = "edit_download.aspx?DownLoadFileId={0}&r={1}";
                    url = string.Format(url, e.Item.Cells[0].Text, Server.UrlDecode(Request.RawUrl));
                    Response.Redirect(url);
                    break;
                default:
                    break;
            }
        }

        protected void dgNews_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header)
            {
                CheckBox cbxAll = e.Item.FindControl("cbxAll") as CheckBox;
                if (cbxAll != null)
                    cbxAll.Attributes.Add("onclick", "javascript:return SelectAll('" + dgNews.ClientID + "');");
            }
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                LinkButton btnDelete = e.Item.FindControl("btnDelete") as LinkButton;
                LinkButton btnModify = e.Item.FindControl("btnModify") as LinkButton;
                Label lblClassType = e.Item.FindControl("lblClassType") as Label;
                DownloadFileType type1 = DownloadFileTypeBLL.GetInstance().Get(new DownloadFileType.Key(int.Parse(lblClassType.Text)));
                lblClassType.Text = type1.TypeName;
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            foreach (DataGridItem gvRow1 in dgNews.Items)
            {
                if (((CheckBox)(gvRow1.FindControl("cbItem"))).Checked)
                {
                    string newid = gvRow1.Cells[0].Text;
                    DownloadFile obj = DownloadFileBLL.GetInstance().Get(new DownloadFile.Key(int.Parse(newid)));
                    DownloadFileBLL.GetInstance().DeleteObj(obj);

                }
            }

            btnSearch_Click(null, null);
        }
    }
}
